package com.xbongbong.parent.util;

import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.saas.enums.SelectProductEnum;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author sunshaoxiang
 * @ClassName FieldUtil
 * @Description 字段工具类
 * @Date 2022/6/20 17:13
 * @Version 1.0
 **/
public class FieldUtil {

    /**
     * 处理选择产品字段
     * @param fieldAttrEntity
     */
    public static void packageSelectProductField(FieldAttrEntity fieldAttrEntity) {
        if (Objects.equals(FieldTypeEnum.SELECT_PRODUCT.getType(),fieldAttrEntity.getFieldType())){
            SubFormPoJo subForm = fieldAttrEntity.getSubForm();
            if (subForm != null && subForm.getItems() != null) {
                List<? extends FieldAttrEntity> subExplainList = subForm.getItems();
                Iterator<? extends FieldAttrEntity> subIterator = subExplainList.iterator();
                while (subIterator.hasNext()){
                    FieldAttrEntity subFieldAttrEntity = subIterator.next();
                    if (Objects.equals(subFieldAttrEntity.getAttr(), SelectProductEnum.PRODUCT.getAttr())){
                        subFieldAttrEntity.setFieldType(FieldTypeEnum.TEXT.getType());
                        break;
                    }
                }
            }
        }
    }

    /**
     * 获取子表单字段attr(这里的子表单包含自定义子表单字段和联系电话系统字段)
     * 重构前 -> com.xbongbong.saas.help.SaasListHelp#filterSubForm(com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO, java.util.Map)
     */
    public static List<String> getSubFormAttrList(Map<String, FieldAttrEntity> explainMap) {
        List<String> subFormAttrList = new ArrayList<>();
        for (Map.Entry<String,FieldAttrEntity> entry : explainMap.entrySet()){
            FieldAttrEntity entity = entry.getValue();
            if (Objects.equals(FieldTypeEnum.SUB_FORM.getType(),entity.getFieldType()) || Objects.equals(FieldTypeEnum.CONTACT_NUMBER.getType(),entity.getFieldType())){
                subFormAttrList.add(entity.getAttr());
            }
        }
        return subFormAttrList;
    }
}
